﻿using System;
using System.Collections.Generic;
using System.Linq;
using IBS.Webparts.PostIt.Code.Interfaces;
using IBS.Webparts.PostIt.Code.Repositories;
using Microsoft.SharePoint;

namespace IBS.Webparts.PostIt.Code.Presenters
{
    public class PostItWebpartPresenter
    {
        private IPostItWebpart _view;
        private IPostItNoteRepository _repository;


        /// <summary>
        /// Creates a new instance of the PostItWebpartPresenter class
        /// </summary>
        /// <param name="view">PostIt Webpart view</param>
        /// <param name="postItListRepository">PostIt List Repository</param>
        public PostItWebpartPresenter(IPostItWebpart view, IPostItNoteRepository postItListRepository)
        {
            _view = view;
            _repository = postItListRepository;
        }


        /// <summary>
        /// Creates a new instance of the PostItWebPartPresenter class
        /// </summary>
        /// <param name="view"></param>
        public PostItWebpartPresenter(IPostItWebpart view) : this(view, new PostItNoteRepository()) { }


        /// <summary>
        /// Initialize the PostIt Webpart Presetner control
        /// </summary>
        public void Initialize()
        {
            // If the List Id is not set, then look up the ID for the default list
            if (_view.ListGuid == new Guid())
            {
                _view.ListGuid = _repository.GetListId(_view.ListName);
                _view.SaveChanges();
            }

            _view.PostItItems = _repository.GetActiveNotes(_view.ListGuid, _view.TitleField, _view.StartDateField, _view.EndDateField);
        }


        /// <summary>
        /// Retrieve the list of options for the List property
        /// </summary>
        /// <returns>List of Options</returns>
        public Dictionary<string, string> GetListOptions()
        {
            return _repository.GetListOptions();
        }


        /// <summary>
        /// Retrieve the list of options for the TitleField property
        /// </summary>
        /// <returns>List of Options</returns>
        public Dictionary<string, string> GetTitleFieldOptions(Guid id)
        {
            return _repository.GetTitleFieldOptions(id);
        }


        /// <summary>
        /// Retrieve the list of options for the date field properties
        /// </summary>
        /// <returns>List of Options</returns>
        public Dictionary<string, string> GetDateFieldOptions(Guid id)
        {
            return _repository.GetDateFieldOptions(id);
        }
    }
}
